@extends('layout.ionic')

@section('title', '图书查询')

@section('content')

    <div id="content">
        @include('app.library.list')
        @include('app.library.record')
        @include('app.library.tabar')
    </div>

@endsection

@section('js')
    @include('app.partial.loading')
    @include('app.partial.alert')
    <script>

        var tabar = new Vue({
            el: '#tabar',
            data:{
                index: 0
            }
        });

        var book = new Vue({
            el: '#search',
            data: {
                loadLock: false,
                isFinish: false,
                keyword: '',
                oldKeyword: '',
                page: 1,
                list: [],
                result: '没有搜索结果~',
                loading: false
            },
            mounted: function(){
//                this.keyword = '测试';
//                this.search(true);
                this.delayHide();
            },
            computed: {
                show: function(){
                    return tabar.index == 0;
                }
            },
            methods:{
                search: function(flag){
                    if(!this.keyword || this.loadLock) return false;

                    this.keyword = this.keyword.trim();

                    if(this.oldKeyword != this.keyword && !flag){
                        this.keyword = this.oldKeyword;
                    }

                    if(flag == true){
                        loading.show = true;
                        this.page = 1;
                    }

                    this.loadLock = true;

                    Vue.http.post("{{ url('library/search') }}", {
                        keyword: this.keyword,
                        page: this.page,
                    }).then(function(response){
                        var length = response.body.length;
                        if(length > 0) {
                            book.list = book.page == 1 ? response.body : book.list.concat(response.body);

                            ++book.page;
                            book.oldKeyword = book.keyword;

                            book.isFinish = length < 20;
                        }else{
                            book.isFinish = true;
                            book.list = [];
                        }
                        loading.show = false;
                        book.loadLock = false;
                    });
                },
                location: function(book){
                    loading.show = true;
                    Vue.http.post("{{ url('library/location') }}", {
                        ctrlRd: book.CtrlRd,
                        ctrlNo: book.CtrlNo
                    }).then(function(response){
                        loading.show = false;

                        if(response.body.status === 'fail'){
                            alertComponent.showAlert('查询错误', response.body.msg);
                            return false;
                        }
                        var data = response.body.data;
                        var stocks = '';
                        if(data.stock.length > 0){
                            for(var i in data.stock){
                                stocks += data.stock[i].code + " " + data.stock[i].status + "\n"
                            }
                        }
                        var info = "《" + book.Title + '》<br>' + data.room + '<br>' + data.floor + data.detail + '<br>' + data.no + '<br>' + stocks;
                        alertComponent.showAlert('书本信息', info)
//                        alert("书本信息:\n《" + book.Title + '》\n' + response.body.room + '\n' + response.body.floor + response.body.detail + '\n' + response.body.no + '\n' + stocks)
                    });
                },
                delayHide: function() {
                    this.loading = true
                }
            }
        });

        var record = new Vue({
            el: '#record',
            data: {
                list: {},
                loading: true,
                isNull: true
            },
            computed: {
                show: function(){
                    return tabar.index == 1;
                }
            },
            mounted: function(){
                Vue.http.get("{{ url('library/reader') }}").then(function(response){
                    //判断是数组还是对象，数组则为空
                    var type = Object.prototype.toString.call(response.body).slice(8, -1);
                    record.isNull = type === 'Object';
                    record.list = response.body;
                    record.loading = false;
                });
            },
            methods: {
            }
        });


    </script>
@endsection